package com.yanqu.road.server.cmd.decoration;

import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.manger.MammonConfigMgr;
import com.yanqu.road.logic.pb.DecorationPb;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserDecoration;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.cross.CrossDataMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.mammon.MammonMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

import java.util.List;

@Cmd(code = Protocol.S_MAMMON_RANK, desc = "财神庙榜单")
public class MammonRankCmd extends AbstractCommand {

	private int decorationId;

	private DecorationProto.MammonListMsg.Builder listMsg;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		DecorationProto.MammonRankReqMsg reqMsg = DecorationProto.MammonRankReqMsg.parseFrom(packet.getBytes());
		decorationId = reqMsg.getDecorationId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(!MammonConfigMgr.isMammonDecorationId(decorationId)){
			ret = GameErrorCode.E_DECORATION_NO_MAMMON;
			return;
		}
		if(!SystemOpenMgr.systemOpen(player, eSystemId.Mammon.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		if (MammonConfigMgr.isCrossMammonDecorationId(decorationId)
				|| MammonConfigMgr.isCrossAreaMammonDecorationId(decorationId)) {
			listMsg = CrossDataMgr.getCrossGroupMammonMsg(decorationId);
		}else {
			List<UserDecoration> decorationList = MammonMgr.getUserDecorationList(decorationId);
			listMsg = DecorationProto.MammonListMsg.newBuilder();
			for(UserDecoration userDecoration : decorationList){
				DecorationProto.MammonTempMsg.Builder msg = DecorationPb.parseMammonTempMsg(userDecoration, UserMgr.parseUserBaseInfo(UserMgr.getUserInfo(userDecoration.getUserId()),
						GameServer.getInstance().getServerId()));
				listMsg.addMammonList(msg);
			}
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		DecorationProto.MammonRankRespMsg.Builder respMsg = DecorationProto.MammonRankRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret) {
			respMsg.setMammonList(listMsg);
		}
		player.sendPacket(Protocol.U_MAMMON_RANK, respMsg);
	}

}
